package com.itheima.set;

import java.util.TreeSet;

public class TreeSetDemo01 {
    public static void main(String[] args) {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student("lin",18));
        treeSet.add(new Student("zhi",20));
        treeSet.add(new Student("kai",16));
        treeSet.add(new Student("lan",16));
        System.out.println(treeSet);

    }
}

class Student implements Comparable<Student> {
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        int result = this.age - o.age;
//        if (result==0){
//            if (this.name.equals(o.name)){
//                return result;
//            }
//        }
        result = result == 0 ? this.name.compareTo(o.name) : result;
//        return this.name.compareTo(o.name);
        return  result;
    }
}
